package com.zzl.car.controller;

import com.zzl.car.entity.User;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.service.UserService;
import com.zzl.car.util.ZdpJwtUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@CrossOrigin
@RestController
public class ZdpLoginController {

    @Autowired
    private UserService userLoginService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
    * 账号密码登录
    * 登录成功 返回 tokenid
    * */
    @PostMapping("/login")
    public Map<String,String> login(@RequestBody Map<String,String> map){
        String telephone = map.get("telephone");
        String password = map.get("password");

        //验证，如果登录成功，返回token给前端
        User user = userLoginService.getUser(telephone, password);
        System.out.println("//登录获取到的用户");
        System.out.println(user);
        if(user!=null){
            //登录成功,解析得到token
            String tokenID = ZdpJwtUtil.generate(telephone);
            //存储用户信息
            redisTemplate.opsForValue().set(telephone,user,2, TimeUnit.HOURS);
            //返回token
            Map<String,String> r = new HashMap<String,String>();
            r.put("tokenID",tokenID);
            return r;
        }else{
            //登录失败，抛出错误
            throw new ZdpException(1002,"用户名或密码错误");
        }
    }

    /*
    * 获取验证码
    * */
    @PostMapping("/verify")
    public String getVerifyCode(@RequestBody Map<String,String> data){
        String email = data.get("email");
        //发送一个信息到消息队列，然后返回验证码以发送的信息给用户
        rabbitTemplate.convertAndSend("verify",email);
        return "验证码已发送";
    }
    /*
    * 注册, 先验证验证码是否正确
    * 再验证手机号是否重复
    * 注册成功 返回 tokenid 和 user
    * */
    @PostMapping("/register")
    public Map<String,Object> regiseter(@RequestBody Map<String,Object>map){

        String verify = (String)map.get("verify");
        String email = (String)map.get("email");
        System.out.println(email);
        System.out.println(verify);
        if(!redisTemplate.opsForValue().get("verify-"+email).equals(verify)){
            throw new ZdpException(1003,"验证码错误");
        }
        System.out.println(email);
        User user = new User();
        user.setEmail(email);
        user.setTelephone((String)map.get("telephone"));
        user.setPassword((String)map.get("password"));
        user = userLoginService.registerUser(user);
        if(user==null){
            throw new ZdpException(1004,"用户名重复");
        }
        Map<String,Object> result = new HashMap<>();
        redisTemplate.opsForValue().set(user.getTelephone(),user,2,TimeUnit.HOURS);
        String tokenID = ZdpJwtUtil.generate(user.getTelephone());
        result.put("tokenID",tokenID);
        result.put("user",user);
        return result;
    }
}
